package com.qcby.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qcby.Constants;
import com.qcby.dao.UserMapper;
import com.qcby.exception.MyRuntimeException;
import com.qcby.model.User;
import com.qcby.model.dto.ToRegistDto;
import com.qcby.model.dto.UserForChangeDto;
import com.qcby.model.dto.UserNoPassword;
import com.qcby.model.resp.RespEntity;
import com.qcby.service.UserService;
import com.qcby.util.StringUtils;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录校验
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public RespEntity<UserNoPassword> login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new IllegalArgumentException("登录参数存在空值！");
        }
        User user = new User();
        if ("admin".equals(username)) {
            user.setURole(Constants.ADMIN_ROLE);
        } else {
            user.setURole(Constants.NORMQAL_ROLE);
        }
        user.setUsername(username);
        user.setPassword(password);
        User loginUser = userService.login(user);
        if (loginUser != null) {
            System.out.println(loginUser.toString());
            UserNoPassword noPassword = new UserNoPassword(loginUser);
            System.out.println(noPassword);
            return RespEntity.successCustomize(Constants.FLAG_200, "登录成功！", noPassword);
        }
        throw new MyRuntimeException(Constants.FLAG_500, "用户不存在或密码错误！");
    }

    /**
     * 页面跳转
     *
     * @return
     */
    @RequestMapping("/toRegist")
    public String toRegist() {
        return "regist";
    }

    /**
     * 注册操作
     *
     * @param toRegistDto
     * @return
     */
    @RequestMapping("/regist")
    @ResponseBody
    public RespEntity<String> regist(@RequestBody ToRegistDto toRegistDto) {
        System.out.println(toRegistDto.toString());
        if (toRegistDto.isResisterParamValid()) {
            Integer resisterResult = userService.insert(new User(toRegistDto));
            if (1 == resisterResult) {
                System.out.println("注册成功");
                return RespEntity.successNoData("注册成功！");
            }
            // null或者0
            System.out.println("注册失败");
            throw new MyRuntimeException(Constants.FLAG_500, "数据库系统异常！");
        } else {
            throw new IllegalArgumentException("注册参数存在空值！");
        }
    }

    /**
     * 判断是否重名
     *
     * @param uid
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/isSameName")
    public RespEntity<Boolean> isSameName(Integer uid, String username) {
        User user = new User();
        user.setUid(uid);
        user.setUsername(username);
        Integer count = userService.isSameName(user);
        if (count == 1) {
            return new RespEntity<Boolean>("", "用户已存在", true);
        } else {
            //用户名不存在，可以成功注册  不知是否保留
            return new RespEntity<Boolean>("", "可以进行注册", false);
        }
    }


    /**
     * 重置密码
     * 先通过查看旧密码是否正确 输入用户名和密码看是否有记录
     * 再修改新密码
     */
    @ResponseBody
    @RequestMapping("/resetPassword")
    public RespEntity<String> resetPassword(Integer uid, String oldPassword, String newPassword) {
        User user = new User();
        user.setUid(uid);
        user.setPassword(oldPassword);
        User userExist = userMapper.selectByPrimaryKey(uid);
        if (userExist == null) {
            return RespEntity.errorNoData(Constants.FLAG_400, "用户不存在！");
        }
        if (!userExist.getPassword().equals(oldPassword)) {
            return RespEntity.errorNoData(Constants.FLAG_400, "旧密码错误！");
        }
        User userNew = new User();
        userNew.setPassword(newPassword);
        userNew.setUid(uid);
        userService.updateByPrimaryKeySelective(userNew);
        return RespEntity.successNoData("修改密码成功!");

    }

    @ResponseBody
    @RequestMapping("/changeInfo")
    public RespEntity<UserNoPassword> changeUserInfo(@RequestBody UserForChangeDto changeDto) {
        Integer uid = changeDto.getUid();
        if (uid != null) {
            User userToChange = new User(changeDto);
            Integer result = userService.updateByPrimaryKeySelective(userToChange);
            if (result != 1) {
                return RespEntity.errorNoData(Constants.FLAG_500, "数据库错误！");
            }
            User exist = userService.selectByPrimaryKey(uid);
            return RespEntity.successCustomize(Constants.FLAG_200, "修改用户信息成功！", new UserNoPassword(exist));
        }
        throw new IllegalArgumentException("用户id不可为空！");
    }
}
